package com.loukou.lkt.lkt.service.impl;


import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.loukou.lkt.lkt.dao.SerialNumberDao;
import com.loukou.lkt.lkt.dao.WarehouseDao;
import com.loukou.lkt.lkt.entity.SerialNumber;
import com.loukou.lkt.lkt.entity.Warehouse;
import com.loukou.lkt.lkt.enums.VoucherPrefixEnum;
import com.loukou.lkt.lkt.service.SerialNumberService;

@Service
public class SerialNumberServiceImpl implements SerialNumberService {

	@Autowired
	private SerialNumberDao serialNumberDao;

	@Autowired
	private WarehouseDao warehouseDao;
	
	@Transactional
	@Override
	public String generateNewVoucherCode(VoucherPrefixEnum voucherPrefixEnum, int warehouseId) {
		String newVoucherCode = "";
		String wareHouseCode = "";
		Warehouse warehouseEntity = warehouseDao.findOne(warehouseId);
		if (warehouseEntity != null) {
			wareHouseCode = warehouseEntity.getWarehouseCode();
		}
		if (wareHouseCode.length() > 0) {
			String yearMonthDay = getYearMonthDay();
			String voucherCodePrefix = voucherPrefixEnum.getName();
			String currentVoucherCode = voucherCodePrefix;
			String voucherCode = voucherCodePrefix + wareHouseCode + yearMonthDay;
			synchronized(this){
				SerialNumber serialNumberEntity = serialNumberDao.findByVoucherTypeAndWarehouseIdAndVoucherCodeLike(
						voucherPrefixEnum.getId(), warehouseId, voucherCode + "%");
				if (serialNumberEntity != null) {
					currentVoucherCode = serialNumberEntity.getVoucherCode();
				}
				newVoucherCode = generateNewCode(currentVoucherCode, wareHouseCode);
				if(serialNumberEntity == null){
					SerialNumber newSerialNumber = new SerialNumber();
					newSerialNumber.setVoucherCode(newVoucherCode);
					newSerialNumber.setVoucherType(voucherPrefixEnum.getId());
					newSerialNumber.setWarehouseId(warehouseId);
					serialNumberDao.save(newSerialNumber);
				}else{
					serialNumberDao.updateSerialNumber(voucherPrefixEnum.getId(), warehouseId, newVoucherCode);
				}
			}
		}
		return newVoucherCode;
	};

	private String generateNewCode(String currentVoucherCode, String wareHouseCode) {
		String newVoucherCode = "";
		String yearMonthDay = "";
		String maxCode = "0001";
		int serialNumberLeng = 4;
		if (currentVoucherCode.length() == 2) {
			yearMonthDay = getYearMonthDay();
			newVoucherCode = currentVoucherCode + wareHouseCode + yearMonthDay + maxCode;
		} else {
			int currentVoucherCodeLength = currentVoucherCode.length();
			String beforeVoucherCode = currentVoucherCode.substring(0, currentVoucherCodeLength - 4);
			String afterVoucherCode = currentVoucherCode.substring(currentVoucherCodeLength - serialNumberLeng,
					currentVoucherCodeLength);
			int newMaxCode = Integer.parseInt(afterVoucherCode) + 1;
			maxCode = String.valueOf(newMaxCode);
			if (maxCode.length() != serialNumberLeng) {
				String strCode = "0000";
				maxCode = strCode.substring(0, serialNumberLeng - maxCode.length()) + maxCode;
			}
			newVoucherCode = beforeVoucherCode + maxCode;
		}
		return newVoucherCode;
	}

	private String getYearMonthDay() {
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
		String stringDate = df.format(new Date());
		return stringDate;
	}
}
